package com.zhu.transform.util;

import okhttp3.*;

import java.io.IOException;
import java.util.Optional;

/**
 * @author yingzhi zhu
 * date 2017/11/17.
 */
public class HttpUtil {
    public static final MediaType JSONHeader
            = MediaType.parse("application/json; charset=utf-8");

    public static String post(String url, String json, String timeStamp, String dataSignature) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSONHeader, json);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("User-Agent", "openApi")
                .addHeader("Content-Type", "application/json; charset=utf-8")
                .addHeader("accept-encoding", "gzip,deflate")
                .addHeader("time-stamp", timeStamp)
                .addHeader("data-signature", dataSignature)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        return Optional.ofNullable(response.body()).orElseThrow(NullPointerException::new).string();
    }
}
